﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net.Mail;

namespace ComTools
{
    public partial class frmMailSend : Form
    {
        public frmMailSend()
        {
            InitializeComponent();
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            System.Net.Mail.SmtpClient client = new SmtpClient(txtSmtpServer.Text.Trim());
            client.EnableSsl = cbxIsEnableSSL.Checked;
            client.UseDefaultCredentials = false;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.Credentials = new System.Net.NetworkCredential(txtUserName.Text.Trim(), txtPwd.Text.Trim());
            //
            var mailFrom = new MailAddress(txtMailFrom.Text, txtUserName.Text, Encoding.UTF8);
            var mailTo = new MailAddress(txtMailTo.Text);
            using (MailMessage message = new MailMessage(mailFrom, mailTo)
            {
                HeadersEncoding = Encoding.UTF8,
                //
                Subject = txtMailSubject.Text,
                SubjectEncoding = Encoding.UTF8,
                //
                BodyEncoding = Encoding.UTF8,
                IsBodyHtml = true,
                Body = rtbMailContent.Text
            })
            {
                if (!string.IsNullOrWhiteSpace(txtMailCC.Text.Trim()))
                {
                    message.CC.Add(txtMailCC.Text.Trim());
                }
                if (!string.IsNullOrWhiteSpace(txtMailBCC.Text.Trim()))
                {
                    message.Bcc.Add(txtMailBCC.Text.Trim());
                }
                client.Send(message);
            }
        }
    }
}
